package com.sky.service.common.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.common.DishFloavorMapper;
import com.sky.mapper.common.DishMapper;
import com.sky.mapper.common.SetmealDishMapper;
import com.sky.pojo.Dish;
import com.sky.pojo.DishFlavor;
import com.sky.result.PageResult;
import com.sky.service.common.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishFloavorMapper dishFloavorMapper;
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<Dish> page = dishMapper.pageQuery(dishPageQueryDTO);
        long total = page.getTotal();
        List<Dish> result = page.getResult();
        return new PageResult(total,result);
    }

    @Override
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        //添加菜品数据
        dishMapper.save(dish);
        //获取save方法返回的主键值
        Long dishId = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if(flavors != null && flavors.size() > 0){
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            dishFloavorMapper.insertBatch(flavors);
        }

    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //查询菜品是否在起售中，如果在起售中，则不能删除
       Integer count = dishMapper.selectStatusById(ids, StatusConstant.ENABLE);
       if(count > 0){
           throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
       }
       //查询菜品是否被套餐关联，如果关联，则不能删除
        Integer count2 = setmealDishMapper.selectByDishId(ids);
       if(count2 > 0){
           throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
       }

       //删除菜品口味数据
       dishFloavorMapper.deleteByDishId(ids);

       //删除菜品数据
        dishMapper.delete(ids);
    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
       DishVO dishVO = dishMapper.getByIdWithFlavor(id);

        return dishVO;
    }

    @Override
    public void updateWithFlavor(DishDTO dishDTO) {

        //1.修改dish表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateWithFlavor(dish);
        //2.修改口味表
        //先删除口味表数据
        dishFloavorMapper.deleteByDishId(Collections.singletonList(dish.getId()));
        //添加新数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(dishFlavor -> {
            dishFlavor.setDishId(dish.getId());
        });
        dishFloavorMapper.insertBatch(flavors);
    }

    @Override
    public void startOrStop(Integer status, Long id) {
        dishMapper.startOrStop(status,id);
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }
//    //我每写一行请帮我生成详细的注解
//    @Override
//    public List<DishVO> listwithFlavor(Dish dish) {
//        List<Dish> list = dishMapper.list(dish);//调用mapper层查询
//        List<DishVO> dishVOList = new ArrayList<>();//创建集合
//
//        for (Dish d : list) {//
//            DishVO dishVO = new DishVO();
//            BeanUtils.copyProperties(d,dishVO);//将d dish对象属性拷贝到dishVO对象中
//
//            //根据菜品id查询对应的口味数据
//            List<DishFlavor> flavors = dishFloavorMapper.getByDishId(d.getId());
//
//            dishVO.setFlavors(flavors);//将口味数据封装到dishVO中
//
//            dishVOList.add(dishVO);
//        }
//        return dishVOList;
//    }
    	/**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFloavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }


}
